﻿@page "/HstoryTaskPage"
@using System.ComponentModel
@using AntDesign.TableModels

@using ZhengMeiJiSettingUI.Common

@inject ChangeServer changeServer
@inject MessageService _message
@inject HstoryTaskServer hstoryTaskServer
@inject MaterialTypeServer materialTypeServer
<div>
    <DatePicker @bind-Value="@dateTime" Picker="@DatePickerType.Date"  />
    <Button @onclick="@Resh" Type="@ButtonType.Primary" Icon="@IconType.Outline.Reload">刷新</Button>


</div>
<div>
    <Table @ref="table"
           TItem="HstoryTaskEntity"
           DataSource="@HstoryTasks"
           Total="_total"
           @bind-PageIndex="_pageIndex"
           @bind-PageSize="_pageSize"
           @bind-SelectedRows="selectedRows"
         
           >
        <Selection Key="@(context.id.ToString())" />
        <Column @bind-Field="@context.id" Sortable />

        <Column Title="转运单" Field="@context.TransshipmentOrder">
            <Input @bind-Value=@context.TransshipmentOrder Disabled="@(true)" />
        </Column>
        <Column Title="任务名" Field="@context.TaskName">
            <Input @bind-Value=@context.TaskName Disabled="@(true)" />
        </Column>
        <Column Title="任务状态" Field=@context.State>
            <Select DataSource="@GlobalValues.TaskStates"
                    AllowClear="true"
                    ValueOnClear="@(0)"
                    @bind-Value="@context.State"
                    ValueName="@nameof(Val_Des.Val)"
                    LabelName="@nameof(Val_Des.Description)" Disabled="@(true)">
            </Select>
        </Column>
        <Column Title="起点" Field=@context.StarDevid>
            <Input @bind-Value=@context.StarDevid Disabled="@(true)" />
        </Column>
        <Column Title="终点" Field=@context.EndDevid>
            <Input @bind-Value=@context.EndDevid Disabled="@(true)" />
        </Column>
        <Column Title="物料名称" Field=@context.MaterialName>
            <Input @bind-Value=@context.MaterialName Disabled="@(true)" />
        </Column>
        <Column Title="物料编码" Field=@context.BarCode>
            <Input @bind-Value=@context.BarCode Disabled="@(true)" />
        </Column>
        <Column Title="物料批次" Field=@context.Batch>
            <Input @bind-Value=@context.Batch Disabled="@(true)" />
        </Column>
        <Column Title="数量" Field=@context.quantity>
            <Input @bind-Value=@context.quantity Disabled="@(true)" />
        </Column>

        <Column Title="创建时间" Field=@context.CreatTime>
            <Input @bind-Value=@context.CreatTime Disabled="@(true)" />
        </Column>

        <Column Title="执行时间" Field=@context.ImplementTime>
            <Input @bind-Value=@context.ImplementTime Disabled="@(true)" />
        </Column>

        <Column Title="取料完成时间" Field=@context.ReclaimerTime>
            <Input @bind-Value=@context.ReclaimerTime Disabled="@(true)" />
        </Column>

        <Column Title="送料开始时间" Field=@context.feedTime>
            <Input @bind-Value=@context.feedTime Disabled="@(true)" />
        </Column>


        <Column Title="完成时间" Field=@context.FinishTine>
            <Input @bind-Value=@context.FinishTine Disabled="@(true)" />
        </Column>

        @*<ActionColumn>
                <Button @onclick="()=>OnChange(context)">更新</Button>
            </ActionColumn>*@

    </Table>

    <br />
    <p>当前页: @_pageIndex | 每页数量: @_pageSize | 总数@(HstoryTasks.Count())</p>

    <br />
    <h5>selections:</h5>
    <Button OnClick="()=> {
                         if (_pageIndex>1)
                         {
                         _pageIndex--;
                         }
                        }">
        上一页
    </Button>
    <Button OnClick="()=> {
               if (_pageIndex < (HstoryTasks.Count() / _pageSize))
                {
                    _pageIndex++;
                }
                }">
        下一页
    </Button>
    <Modal Title="@title"
           Visible="@_visible"
           OnOk="@HandleOk"
           OnCancel="@HandleCancel">
        <p>确认删除？</p>
    </Modal>

</div>

@using System.Text.Json;
@code {
    private bool ReshFlag = false;
    ObservableCollection<HstoryTaskEntity> HstoryTasks { get; set; } = new ObservableCollection<HstoryTaskEntity>();
    ObservableCollection<MaterialTypeEntity> MaterialTypes { get; set; } = new ObservableCollection<MaterialTypeEntity>();
    IEnumerable<HstoryTaskEntity> selectedRows;
    ITable table;
    DateTime? dateTime;
    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;
    string title = "危险提示！！！";
    bool _visible = false;



    private async void HandleOk(MouseEventArgs e)
    {
        @foreach (var selected in selectedRows)
        {
            bool flag = await changeServer.Delete(selected.id);
            if (flag)
            {
                //_message.Info($"CODE{selected.DevId}-删除成功");
            }
            else
            {
                //_message.Info($"CODE{selected.DevId}-删除失败");
            }
        }
        selectedRows = null;
        _visible = false;
        await init();
    }

    private void HandleCancel(MouseEventArgs e)
    {
        selectedRows = null;
        _visible = false;
    }

    protected override async Task OnInitializedAsync()
    {
        var list = await materialTypeServer.GetMaterialType();
        if (list != null)
        {
            foreach (var item in list)
            {
                MaterialTypes.Add(item);
            }
            var List = await hstoryTaskServer.GetHstoryTask(DateTime.Now.ToString("yyyy/M/d"));

            if (List != null)
            {
                foreach (var item in List)
                {
                    HstoryTasks.Add(item);
                }
            }
        }
        base.OnInitialized();
    }


    public async void OnChange(ChangeEntity Change)
    {
        //bool flag = await changeServer.UpData(Change);
        //if (flag)
        //{
        //    _message.Info($"CODE{Change.DevId}-更新成功");
        //}
        //else
        //{
        //    _message.Info($"CODE{Change.DevId}-更新失败");
        //}
        //init();
    }

    public void RemoveSelection(int id)
    {
        // var selected = selectedRows.Where(x => x.Id != id);
        // selectedRows = selected;
    }

    private void Delete(int id)
    {
        //forecasts = forecasts.Where(x => x.Id != id).ToArray();
        // _total = forecasts.Length;
    }
    private async void Resh()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;

            await init();
            ReshFlag = false;
        }

    }

    private async Task Clear()
    {
        if (!ReshFlag)
        {
            ReshFlag = true;
            await init();
            ReshFlag = false;
        }
    }

    async Task init()
    {
        var time = dateTime?.ToString("yyyy/M/d");
        if (time==null)
        {
            time = DateTime.Now.ToString("yyyy/M/d");
        }
        var list = await materialTypeServer.GetMaterialType();
        if (list != null)
        {
            MaterialTypes.Clear();
            foreach (var item in list)
            {
                MaterialTypes.Add(item);
            }
            var List = await hstoryTaskServer.GetHstoryTask(time);
            if (List != null)
            {
                _pageIndex = 1;
                HstoryTasks.Clear();
                foreach (var item in List)
                {
                    HstoryTasks.Add(item);
                }
            }
        }
        await this.InvokeAsync(() =>
        {
            this.StateHasChanged();
        });
    }

}


